package com.Yourweibo.util;

import java.io.Serializable;
import java.util.List;

import weibo4android.Count;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ZoomControls;

import com.Yourweibo.R;
import com.Yourweibo.UserTimelineActivity;
import com.Yourweibo.status.StatusInformation;
import com.Yourweibo.status.TimeLineConstant;
import com.Yourweibo.util.HttpLoadAsync.HttpCallBack;

/**
 * @author root
 *
 */
/**
 * @author root
 *
 */
public class DataProcessing {
	
	

 
	/**
	 * 当前状态格式化
	 * @param status
	 * @return
	 */
	public  static SpannableString statusSetHighligth(Status status){
		String sname=status.getUser().getName();
		String stext=status.getText();
		final String stoalText=sname+":"+stext;
		SpannableString spannedString=new SpannableString(stoalText);
		//设置人名
		int indexStart=0;
		int indexEnd=sname.length();
		spannedString.setSpan(new ClickableSpan(){

			@Override
			public void onClick(View widget) {
				// TODO Auto-generated method stub
				
			}}, indexStart, indexEnd, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
		//设置@
 
	 	for(int i=0;i<stoalText.length();i++){
			char pointerCharStart=stoalText.charAt(i);
			if(pointerCharStart=='@'){
				indexStart=i;
 
				for(int j=i;j<=stoalText.length();j++){
					if((stoalText.charAt(j)==' ')||j==stoalText.length()-1||(stoalText.charAt(j)==':')||(stoalText.charAt(j)=='：')){
						indexEnd=j;
				 
						final String  AtSubString=stoalText.substring(indexStart+1, indexEnd);
				 
						ClickableSpan clickspan= new ClickableSpan(){

							@Override
							public void onClick(View widget) {
								// TODO Auto-generated method stub
								
					 
								widget.setTag(AtSubString);
				 
							    showUserTimeLine(widget);
							}};
						spannedString.setSpan(clickspan, indexStart, indexEnd+1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
				 
						i=j;
						break;
					}
					
					
				}
				
			} 
	}
	 
		//设置#
		 indexStart=0;
		indexEnd=0;
		for(int i=0;i<stoalText.length();i++){
			char pointerCharStart=stoalText.charAt(i);
			if(pointerCharStart=='#'){
				indexStart=i;
 
				for(int j=i+1;j<=stoalText.length();j++){
					if(stoalText.charAt(j)=='#'){
						indexEnd=j;
						spannedString.setSpan(new ClickableSpan(){

							@Override
							public void onClick(View widget) {
								// TODO Auto-generated method stub
								
							}}, indexStart, indexEnd+1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
						i=j;
						break;
					}
					
					
				}
				
			} 
		
		}
		
		
		
		 
		
		
		
		
		return spannedString;
	}
	
	
	public static void showUserTimeLine(final View widget){
		String progressDialogMessage=(String)widget.getTag()+" 的微博正在载入中...";
		 
		HttpLoadAsync httpload=new HttpLoadAsync(widget.getContext(),progressDialogMessage);
		httpload.execute(new HttpCallBack(){

			@Override
			public Intent Loading() {
				// TODO Auto-generated method stub
				Intent intent=new Intent(widget.getContext(),UserTimelineActivity.class);
				 List<Status> FriendsTimelineList = null;
				 List<Count> TimelineListCount=null;
				 Weibo weibo=TimeLineConstant.getInstance().getWeibo(); 
				 try {
					FriendsTimelineList=weibo.getUserTimeline((String)widget.getTag());
 
				} catch (WeiboException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				 
				 intent.putExtra("Timeline",(Serializable)FriendsTimelineList);
				 intent.putExtra("TimelineCount",(Serializable)TimelineListCount);
				 intent.putExtra("UserName",(String) widget.getTag());
				 return intent;
			}});
	
	 
		
	}
	private static  	ImageLoadAsync imageLoader=new ImageLoadAsync();
	
 
	/**
	 * 加载图片
	 * @param url  图片地址
	 * @param v    放入图片的imageview
	 * @param convertView 当前的view
	 */
	public static void loadHttpImage(final String url,final ImageView v,Context context){

		Drawable cacheImage = imageLoader.LoadDrawalbe(url,
				v, new ImageLoadAsync.ImageCallback() {

					@Override
					public void imageLoaded(Drawable imageDrawable,
							ImageView imageView, String imageUr) {
						// TODO Auto-generated method stub
						System.out.println("&&&&&&&&&&&&&&&&");
						if(v.getTag()==url){
						imageView.setImageDrawable(imageDrawable);
					    }
					}

				});

		if (cacheImage != null) {	

			v.setImageDrawable(cacheImage);
			
	
			
		} else {

			v.setImageDrawable(context.getResources()
					.getDrawable(R.drawable.timeline_default_photo1));

		}
		
		
	}
 
	/**
	 * 加载大图片
	 * @param convertView 当期view
	 * @param uri 图片地址
	 *  
	 */
	public static void LoadlargeImage(View view,String uri){
		Builder db=new AlertDialog.Builder(view.getContext());
		 
		ImageView iv=new ImageView(view.getContext());
		iv.setTag(uri);
		DataProcessing.loadHttpImage(uri,iv,view.getContext());
		
		iv.setMinimumHeight(iv.getDrawable().getIntrinsicHeight()*4);
		iv.setMaxWidth(iv.getDrawable().getIntrinsicWidth()*4);
		db.setView(iv );
 
		db.show();
		
	}
	public static void LoadlargeImage2(View view,String uri){
		final Builder db=new AlertDialog.Builder(view.getContext());
		final View dialaogView=View.inflate(view.getContext(), R.layout.status_image_container, null);
		final ImageView iv=(ImageView)dialaogView.findViewById(R.id.status_image_imageview);
		ZoomControls zc=(ZoomControls)dialaogView.findViewById(R.id.status_image_zoomControls1);
		
		
		iv.setTag(uri);
		DataProcessing.loadHttpImage(uri,iv,view.getContext());
		
		
		zc.setOnZoomInClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				db.setView(null);
				iv.setMinimumHeight(iv.getDrawable().getIntrinsicHeight()*4);
				iv.setMaxWidth(iv.getDrawable().getIntrinsicWidth()*4);
				dialaogView.postInvalidate();
				db.setView(dialaogView );
			}});
		
;			db.setView(dialaogView );
 
		db.show();
		
	}

}
